package com.cgwx.springsecurity.config;

import com.cgwx.springsecurity.custom.CustomMd5Password;
import com.cgwx.springsecurity.filter.TokenAuthenticationFilter;
import com.cgwx.springsecurity.filter.TokenLoginFilter;
import com.cgwx.springsecurity.service.SysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author GaoWei
 * @Date 2023/10/21 - 10:45
 */
@Configuration
@EnableWebSecurity //@EnableWebSecurity是开启SpringSecurity的默认行为
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启注解功能，默认禁用注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private CustomMd5Password customMd5Password;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysLoginLogService sysLoginLogService;

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 这是配置的关键，决定哪些接口开启防护，哪些接口绕过防护
        http
            // CSRF禁用，因为不使用session
            .csrf().disable()
            // 开启跨域以便前端调用接口
            .cors().and()
            // 过滤请求
            .authorizeRequests()
            // 指定某些接口不需要通过验证即可访问。登陆接口肯定不需要认证
            .antMatchers("/admin/system/index/login").permitAll()
            // 其它所有接口需要认证才能访问
            .anyRequest().authenticated()
            .and()
            //TokenAuthenticationFilter放到UsernamePasswordAuthenticationFilter的前面，这样做就是为了除了登录的时候去查询数据库外，其他时候都用token进行认证。
            .addFilterBefore(new TokenAuthenticationFilter(redisTemplate), UsernamePasswordAuthenticationFilter.class)
            .addFilter(new TokenLoginFilter(authenticationManager(), redisTemplate, sysLoginLogService));
            //最大终端数量为1
//            .sessionManagement().maximumSessions(1);

        //禁用session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 指定UserDetailService和加密器
        auth.userDetailsService(userDetailsService).passwordEncoder(customMd5Password);
    }

    /**
     * 配置哪些请求不拦截
     * 排除swagger相关请求
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/favicon.ico","/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html");
    }
}
